﻿using BZOrderPrint.BLL.Helper;
using BZOrderPrint.BLL.Model;
using BZOrderPrint.DAL;
using BZOrderPrint.DAL.Entity;
using BZOrderPrint.PDF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static BZOrderPrint.DAL.AppEnum;

namespace BZOrderPrint
{
    public partial class OrderDetailFoim : Form
    {
        private BZContext context;
        OrderDetailModel orderDetail;
        public OrderDetailFoim()
        {
            InitializeComponent();
        }

        public OrderDetailFoim(OrderDetailModel model)
        {
            orderDetail = model;
            InitializeComponent();
            Init();
        }



        private void OrderDetailFoim_Load(object sender, EventArgs e)
        {
            context = new BZContext(ConfigHelper.ConnString);
        }

        private void Init()
        {
            this.orderCode.Text = orderDetail.order.OrderCode;
            this.orderType.Text = EnumHelper.GetDescription((OrderType)orderDetail.order.OrderType);
            this.shopName.Text = orderDetail.order.OrderShopName;
            this.orderTime.Text = orderDetail.order.OrderDateTime?.ToString("yyyy-MM-dd HH:mm:ss");
            this.orderStatus.Text = EnumHelper.GetDescription((OrderStatus)orderDetail.order.OrderStatus);
            this.shipMethod.Text = EnumHelper.GetDescription((ShipMethodType)orderDetail.order.ShipMethod);
            this.payType.Text = EnumHelper.GetDescription((PayType)orderDetail.order.PayType);
            this.memberNumber.Text = orderDetail.order.MemberNumber;
            this.phone.Text = orderDetail.order.Phone;
            this.address.Text = orderDetail.order.Address;
            this.orderPrice.Text = orderDetail.order.OrderPrice.ToString();


            var data = new DataTable();
            var dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "ID",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "订单号",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "门店",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "商品编码",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "商品名称",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "商品单价",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "数量",
                DefaultValue = "",
            };
            data.Columns.Add(dc);

            if (orderDetail.orderItems.Count > 0)
            {
                foreach (var info in orderDetail.orderItems)
                {
                    var dataRow = data.NewRow();
                    dataRow["ID"] = info.Id;
                    dataRow["订单号"] = orderDetail.order.OrderCode;
                    dataRow["门店"] = orderDetail.order.OrderShopName;
                    dataRow["商品编码"] = info.OrderItemCode;
                    dataRow["商品名称"] = info.OrderItem;
                    dataRow["商品单价"] = info.Price;
                    dataRow["数量"] = info.Quantity;
                    data.Rows.Add(dataRow);
                }
            }
            DataGridViewButtonColumn buttonColum = new DataGridViewButtonColumn();
            buttonColum.Text = "查看";
            buttonColum.Name = "查看";
            buttonColum.ToolTipText = "查看";
            buttonColum.ReadOnly = false;
            buttonColum.Width = 40;
            ItemGridView.Columns.Insert(0, buttonColum);
            ItemGridView.DataSource = data;
            ItemGridView.ReadOnly = false;
            ItemGridView.Columns[0].Width = 60;
            ItemGridView.Columns[1].Width = 60;
            ItemGridView.Columns[2].Width = 150;
            ItemGridView.Columns[3].Width = 170;
            ItemGridView.Columns[4].Width = 180;
            ItemGridView.Columns[5].Width = 250;
            ItemGridView.Columns[6].Width = 70;
            ItemGridView.Columns[7].Width = 70;

            for (int i = 0; i <= ItemGridView.Columns.Count - 1; i++)
            {
                ItemGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (!PrintHelper.Verification())
            {
                MessageBox.Show("请设置打印机");
                return;
            }
            try
            {
                PrintHelper.FaceSheetPrinter();
                if (orderDetail.order.IsGroup == 0)
                    LabelHelper.PrintShopLabel(orderDetail.order);
                else
                    LabelHelper.PrintTeamLabel(orderDetail.order);

                MessageBox.Show("打印完成");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                return;
            }

        }
        private BzOrderItemEntity GetOrder(DataGridViewRow data)
        {
            return context.bzOrderItemEntities.FirstOrDefault(x => x.Id == int.Parse(data.Cells["ID"].Value.ToString()));
        }
        private void ItemGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewColumn column = ItemGridView.Columns[e.ColumnIndex];
            if (column is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                int id = int.Parse(ItemGridView.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                var item = context.bzOrderItemEntities.FirstOrDefault(x => x.Id == id);
                UpdateItem update = new UpdateItem(item);
                update.Show();
            }
        }
    }
}
